package com.attendancesystem.controller;

import com.attendancesystem.model.Course;
import com.attendancesystem.service.CourseService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "CourseInfoController", value = "/CourseInfoController")
public class CourseInfoController extends HttpServlet {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            CourseService courseService = new CourseService();
            List<Course> courses = courseService.getCourseList();
//            System.out.println("course:"+courses);


            objectMapper.writeValue(response.getWriter(), courses);

        }catch (Exception e){
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            objectMapper.writeValue(response.getWriter(),
                    Collections.singletonMap("error", "服务器内部错误"));
        }






    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
